package com.sevattal.service.leaves.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sevattal.common.base.result.ResultCodeEnum;
import com.sevattal.service.base.exception.SevattalException;
import com.sevattal.service.leaves.entity.ReturnChildOrder;
import com.sevattal.service.leaves.entity.ReturnMainOrder;
import com.sevattal.service.leaves.entity.vo.AuditOrderVo;
import com.sevattal.service.leaves.entity.vo.ReturnChildOrderVo;
import com.sevattal.service.leaves.entity.vo.ReturnMainOrderVo;
import com.sevattal.service.leaves.entity.vo.ReturnOrderVo;
import com.sevattal.service.leaves.mapper.ReturnChildOrderMapper;
import com.sevattal.service.leaves.mapper.ReturnMainOrderMapper;
import com.sevattal.service.leaves.service.ReturnOrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 归还子表 服务实现类
 * </p>
 *
 * @author Sevattal
 * @since 2021-08-28
 */
@Service
public class ReturnOrderServiceImpl  implements ReturnOrderService {

    @Autowired
    private ReturnMainOrderMapper mainOrderMapper;

    @Autowired
    private ReturnChildOrderMapper childOrderMapper;

    @Override
    public boolean createReturnOrder(ReturnOrderVo returnOrderVo) {
        if (returnOrderVo.getFRmid() ==null || StringUtils.isEmpty(returnOrderVo.getFRmid()) ||
        returnOrderVo.getReturnChildOrderVoList().size() == 0) {
            throw new SevattalException(ResultCodeEnum.PARAM_ERROR);
        }
        QueryWrapper<ReturnMainOrder> mainOrderQueryWrapper = new QueryWrapper<>();
        mainOrderQueryWrapper.eq("f_rmId",returnOrderVo.getFRmid());
        int count = mainOrderMapper.selectCount(mainOrderQueryWrapper);
        if (count > 0) {
            throw  new SevattalException(ResultCodeEnum.PARAM_ERROR);
        }
        mainOrderMapper.insert(getReturnMainOrder(returnOrderVo));
        List<ReturnChildOrder> returnChildOrderList = getReturnChildOrder(returnOrderVo);
        for (int i = 0; i< returnChildOrderList.size(); i++){
            childOrderMapper.insert(returnChildOrderList.get(i));
        }
        return true;
    }

    @Override
    public boolean updateReturnOrder(ReturnOrderVo returnOrderVo) {
        if (returnOrderVo.getFRmid() ==null || StringUtils.isEmpty(returnOrderVo.getFRmid()) ||
                returnOrderVo.getReturnChildOrderVoList().size() == 0) {
            throw new SevattalException(ResultCodeEnum.PARAM_ERROR);
        }
        QueryWrapper<ReturnMainOrder> mainOrderQueryWrapper = new QueryWrapper<>();
        mainOrderQueryWrapper.eq("f_rmId",returnOrderVo.getFRmid());
        int count = mainOrderMapper.selectCount(mainOrderQueryWrapper);
        if (count == 0) {
            throw  new SevattalException(ResultCodeEnum.PARAM_ERROR);
        }
        mainOrderMapper.update(getReturnMainOrder(returnOrderVo),mainOrderQueryWrapper);

        QueryWrapper<ReturnChildOrder> childOrderQueryWrapper = new QueryWrapper<>();
        childOrderQueryWrapper.eq("f_rmId",returnOrderVo.getFRmid());
        childOrderMapper.delete(childOrderQueryWrapper);
        List<ReturnChildOrder> returnChildOrderList = getReturnChildOrder(returnOrderVo);
        for (int i = 0; i< returnChildOrderList.size(); i++){
            childOrderMapper.insert(returnChildOrderList.get(i));
        }
        return true;
    }

    @Override
    public boolean deleteReturnOrder(String id) {
        QueryWrapper<ReturnMainOrder> mainOrderQueryWrapper = new QueryWrapper<>();
        mainOrderQueryWrapper.eq("f_rmId",id);
        QueryWrapper<ReturnChildOrder> childOrderQueryWrapper = new QueryWrapper<>();
        childOrderQueryWrapper.eq("f_rmId",id);
        mainOrderMapper.delete(mainOrderQueryWrapper);
        childOrderMapper.delete(childOrderQueryWrapper);
        return true;
    }

    @Override
    public List<ReturnOrderVo> getReturnOrderList(ReturnOrderVo returnOrderVo) {
        QueryWrapper<ReturnMainOrder> mainOrderQueryWrapper = new QueryWrapper<>();
        mainOrderQueryWrapper.orderByAsc("rm.f_rmId");
        mainOrderQueryWrapper.eq("rm.is_disabled",0);
        mainOrderQueryWrapper.eq("rm.is_deleted",0);
        if (returnOrderVo.getFRmid() != null && !StringUtils.isEmpty(returnOrderVo.getFRmid())){
            mainOrderQueryWrapper.like("rm.f_rmId",returnOrderVo.getFRmid());
        }
        if (returnOrderVo.getFEname() != null && !StringUtils.isEmpty(returnOrderVo.getFEname())){
            mainOrderQueryWrapper.like("e1.f_eName",returnOrderVo.getFEname());
        }
        if (returnOrderVo.getFWhid() != null && !StringUtils.isEmpty(returnOrderVo.getFWhid())){
            mainOrderQueryWrapper.eq("rm.f_whId",returnOrderVo.getFWhid());
        }
        if (returnOrderVo.getSearchStartDate() != null){
            mainOrderQueryWrapper.ge("rm.f_rmDate",returnOrderVo.getSearchStartDate());
        }
        if (returnOrderVo.getSearchEndDate() != null){
            mainOrderQueryWrapper.le("rm.f_rmDate",returnOrderVo.getSearchEndDate());
        }
        List<ReturnMainOrderVo>  returnMainOrderVos = mainOrderMapper.getReturnMainOrderList(mainOrderQueryWrapper);

        List<ReturnOrderVo> returnOrderVoList = new ArrayList<>();
        QueryWrapper<ReturnChildOrder> childOrderQueryWrapper = new QueryWrapper<>();
        for (int i =0;i<returnMainOrderVos.size();i++){
            childOrderQueryWrapper.eq("rc.f_rmId",returnMainOrderVos.get(i).getFRmid());
            childOrderQueryWrapper.eq("rc.is_disabled",0);
            childOrderQueryWrapper.eq("rc.is_deleted",0);
            List<ReturnChildOrderVo> returnChildOrderVos = childOrderMapper.getReturnChildOrderList(childOrderQueryWrapper);
            returnOrderVoList.add(getReturnOrderVo(returnMainOrderVos.get(i),returnChildOrderVos));
            childOrderQueryWrapper.clear();
        }
        return returnOrderVoList;
    }

    // 从vo 获取主表信息
    public ReturnMainOrder getReturnMainOrder(ReturnOrderVo returnOrderVo){
        ReturnMainOrder returnMainOrder = new ReturnMainOrder();
        returnMainOrder.setFRmid(returnOrderVo.getFRmid());
        returnMainOrder.setFEid(returnOrderVo.getFEid());
        returnMainOrder.setFWhkeeperid(returnOrderVo.getFWhkeeperid());
        returnMainOrder.setFWhid(returnOrderVo.getFWhid());
        returnMainOrder.setFRmdate(returnOrderVo.getFRmdate());
        returnMainOrder.setFRmstatus(returnOrderVo.getFRmstatus());
        returnMainOrder.setFRmconfirm(returnMainOrder.getFRmconfirm());
        returnMainOrder.setFRmmemo(returnOrderVo.getFRmmemo());
        return  returnMainOrder;
    }
    // 从vo 获取子表信息
    public List<ReturnChildOrder> getReturnChildOrder(ReturnOrderVo returnOrderVo){
        List<ReturnChildOrder> returnChildOrderList = new ArrayList<>();
        ReturnChildOrder child_temp = new ReturnChildOrder();
        for (int i = 0; i<returnOrderVo.getReturnChildOrderVoList().size();i++){
            child_temp.setFRmid(returnOrderVo.getFRmid());
            child_temp.setFMid(returnOrderVo.getReturnChildOrderVoList().get(i).getFMid());
            child_temp.setFRcquantity(returnOrderVo.getReturnChildOrderVoList().get(i).getFRcquantity());
            child_temp.setFRcdate(returnOrderVo.getReturnChildOrderVoList().get(i).getFRcdate());
            child_temp.setFRcstatus(returnOrderVo.getReturnChildOrderVoList().get(i).getFRcstatus());
            child_temp.setFRcmemo(returnOrderVo.getReturnChildOrderVoList().get(i).getFRcmemo());
            returnChildOrderList.add(child_temp);
        }
        return  returnChildOrderList;
    }
    // 拼接 Vo
    public ReturnOrderVo getReturnOrderVo(ReturnMainOrderVo returnMainOrderVo,List<ReturnChildOrderVo> childOrderVos){
        ReturnOrderVo returnOrderVo = new ReturnOrderVo();
        returnOrderVo.setFRmid(returnMainOrderVo.getFRmid());
        returnOrderVo.setFEid(returnMainOrderVo.getFEid());
        returnOrderVo.setFEname(returnMainOrderVo.getFEname());
        returnOrderVo.setFWhkeeperid(returnMainOrderVo.getFWhkeeperid());
        returnOrderVo.setFWhkeepername(returnMainOrderVo.getFWhkeepername());
        returnOrderVo.setFWhid(returnMainOrderVo.getFWhid());
        returnOrderVo.setFWhname(returnMainOrderVo.getFWhname());
        returnOrderVo.setFRmdate(returnMainOrderVo.getFRmdate());
        returnOrderVo.setFRmstatus(returnMainOrderVo.getFRmstatus());
        returnOrderVo.setFRmconfirm(returnMainOrderVo.getFRmconfirm());
        returnOrderVo.setFRmmemo(returnMainOrderVo.getFRmmemo());
        returnOrderVo.setReturnChildOrderVoList(childOrderVos);
        return returnOrderVo;
    }
}
